﻿using LD.Common;
using ReaLTaiizor.Controls;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using YTE.Control;

namespace LD.BC
{
    public partial class FrmIoManual : FrmBase
    {
        private string id = Guid.NewGuid().ToString();
        public FrmIoManual()
        {
            InitializeComponent();
            GetLang(this.Controls);
            this.FormClosing += FrmIoManual_FormClosing;
            this.Shown += FrmIoManual_Shown;
        }

        private void GetLang(Control.ControlCollection _controls)
        {
            foreach (Control control in _controls)
            {
                GetLang(control.Controls);
                if (control is BigLabel)
                {
                    control.Text = Language.GetString("FrmIoManual." + control.Name);
                }
            }
        }

        private void FrmIoManual_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();

            PubSub.Hub.Default.Unsubscribe(id);
        }

        private void FrmIoManual_Shown(object sender, EventArgs e)
        {
            Init();
            timer1.Start();
            //Subscribe();
        }
        private void Subscribe()
        {
            PubSub.Hub.Default.Subscribe<MonitorModel>(id, (data) => Monitor(data));
        }
        private void Monitor(MonitorModel data)
        {
            if (data == null)
                return;
            if (!this.IsDisposed)
            {
                foreach (var item in data.DI)
                {
                    var control = this.panel_di.Controls.Find("di" + item, false);
                    if (control.Count() > 0)
                    {
                        if (control[0] != null && control[0].GetType() == typeof(IoControl))
                        {
                            var ioControl = (control[0] as IoControl);
                            if (!this.IsDisposed)
                            {
                                this.Invoke(new Action(() =>
                                {
                                    ioControl.Signal = item;
                                    ioControl.Refresh();
                                }));
                            }
                        }
                        else
                            Console.WriteLine("IO控件未找到");
                    }
                }

                foreach (var item in data.DI)
                {
                    var control = this.panel_di.Controls.Find("do" + item, false);
                    if (control.Count() > 0)
                    {
                        if (control[0] != null && control[0].GetType() == typeof(IoControl))
                        {
                            var ioControl = (control[0] as IoControl);
                            this.Invoke(new Action(() =>
                            {
                                ioControl.Signal = item;
                            }));
                        }
                        else
                            Console.WriteLine("IO控件未找到");
                    }
                }
            }
        }
        private void Monitor()
        {
            #region IO
            var inList = BcFactory.Instance.Control.GetIn();

            int index = 1;
            foreach (var item in inList)
            {
                var control = this.panel_di.Controls.Find("di" + index, true);
                if (control.Count() > 0)
                {
                    if (control[0] != null && control[0].GetType() == typeof(IoControl))
                    {
                        var ioControl = (control[0] as IoControl);
                        if (!this.IsDisposed)
                        {
                            ioControl.Signal = item;
                        }
                    }
                    else
                        Console.WriteLine("IO控件未找到");
                }
                index++;
            }
            //out
            var outList = BcFactory.Instance.Control.GetOut();
            index = 1;
            foreach (var item in outList)
            {
                var control = this.panel_di.Controls.Find("do" + index, true);
                if (control.Count() > 0)
                {
                    if (control[0] != null && control[0].GetType() == typeof(IoControl))
                    {
                        var ioControl = (control[0] as IoControl);
                        if (!this.IsDisposed)
                        {
                            ioControl.Signal = item;
                        }
                    }
                    else
                        Console.WriteLine("IO控件未找到");
                }
                index++;
            }
            #endregion
        }
        private void Init()
        {
            var _di = BcFactory.Instance.Config.IoList.Where(p => p.IoType == (int)IoType.输入)
                                                      .OrderBy(p => p.No).ToList();
            int x = 5; int y = 40;
            foreach (var item in _di)
            {
                var control = GetIoControl("di" + item.No, item.No, item.Name);
                control.IoType = IoType.输入;
                control.Size = new Size(240, 40);
                control.BorderStyle = BorderStyle.FixedSingle;
                control.Location = new Point(x, y);
                this.panel_di.Controls.Add(control);
                y += 45;
            }

            var _do = BcFactory.Instance.Config.IoList.Where(p => p.IoType == (int)IoType.输出)
                                                      .OrderBy(p => p.No).ToList();
            x = 250; y = 40;
            foreach (var item in _do)
            {
                var control = GetIoControl("do" + item.No, item.No, item.Name);
                control.BorderStyle = BorderStyle.FixedSingle;
                control.Size = new Size(240, 40);
                control.IoType = IoType.输出;
                control.Location = new Point(x, y);
                this.panel_di.Controls.Add(control);
                y += 45;
            }
        }
        private IoControl GetIoControl(string name, int no, string ioName)
        {
            IoControl control = new IoControl();
            control.Name = name;
            control.No = no;
            control.IoName = ioName;
            control.Size = new Size(290, 40);
            return control;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            Monitor();
            timer1.Start();
        }
    }
}
